package com.cennavi.minemapdemo.activity.overlayer;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.util.Config;
import com.cennavi.minemapdemo.util.RandomUtil;
import com.minedata.minemap.MinemapAccountManager;
import com.minedata.minemap.camera.CameraPosition;
import com.minedata.minemap.geometry.LatLng;
import com.minedata.minemap.map.MapView;
import com.minedata.minemap.map.MineMap;
import com.minedata.minemap.overlay.MarkerLayer;
import com.minedata.minemap.overlay.MarkerLayerOptions;

public class MarkerLayer2Activity extends AppCompatActivity {

    private MapView mapView;
    private static MineMap mineMap = null;
    private SeekBar mSeekBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MinemapAccountManager.getInstance(this, Config.MAP_TOK, Config.OFFLINE_TOK, Config.MAP_ID);

        setContentView(R.layout.activity_marklayer_test);
        mapView = (MapView) findViewById(R.id.mapView);
        mSeekBar = (SeekBar) findViewById(R.id.seekbar);
        mSeekBar.setVisibility(View.GONE);

        //mine map sdk 2.0.6之前生效
        // mapView.setStyleUrl("http://minedata.cn/service/solu/style/id/5699");
        mapView.setApiBaseUrl(Config.API_BASE_URL);
        mapView.setApiUrl(Config.API_URL);
        mapView.setSpriteImageUrl(Config.SPRITE_GLYPHS_URL);
        mapView.addMapRenderCallback(new MapView.OnMapReadyListener() {
            @Override
            public void onMapReady(MineMap omineMap) {
                mineMap = omineMap;
                mineMap.setRepaint(false);
                mineMap.getUiSettings().setCompassEnabled(true);
                mineMap.getUiSettings().setRotateGesturesEnabled(true);
                mineMap.setStyleUrl(Config.BASE_URL + Config.MAP_ID, new MineMap.OnStyleLoadedListener() {
                    @Override
                    public void onStyleLoaded(String s) {

                        //mine map sdk 2.0.6及之后版本这样使用
                        //layers 和 images 都在这里面加载
                        addImageToMap();

                        addLayersToMap();
                    }
                });
                mineMap.setCameraPosition(
                        new CameraPosition.Builder().target(new LatLng(Config.mapCentrelat,Config.mapCentrelon)).zoom(17.3)
                                .tilt(0)//设定倾斜角度
                                .bearing(0) //摄像机指向的方向,从北部顺时针方向设置
                                .zoom(9)
                                .build());
                mineMap.setMaxZoomPreference(21);//设置最高展示级别，最高为21级别
                mineMap.setMinZoomPreference(6);//设置最低的展示级别
//                mineMap.setDebugActive(true);


            }

        });

        initView();

    }

    private void initView() {

        Button test = findViewById(R.id.button111);
        test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeStyle();
            }
        });
    }

    //加载可能的图片资源
    private void addImageToMap() {

        Bitmap bitmap = getTextBitmap(Color.parseColor("#ffffff"), 20);
        mineMap.addImage("icon_img", bitmap);

        icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher22);
        mineMap.addImage(R.mipmap.ic_launcher22 + "", icon);
    }

    private void addLayersToMap() {

        MarkerLayerOptions options = new MarkerLayerOptions("MarkerLayer")
                .textColor("#6495ED")
                .textHaloColor("#6495ED")
                .textHaloWidth(0.2f);
        options.textIgnorePlacement(false);
        options.textAllowOverlap(false);
        options.iconTextFit(MarkerLayerOptions.MARKER_ICON_TEXT_FIT_BOTH);
        options.iconTextFitPadding(new Float[]{10f, 10f, 10f, 10f});
        markerLayer = mineMap.addLayer(options);

        markerLayer.add(new MarkerLayerOptions.MarkerItem().textName("市府广场")
                .position(new LatLng(41.804446, 123.434079))
                .icon("icon_img"));

        LatLng latLng = RandomUtil.randomLatLon();
        markerItem = new MarkerLayerOptions.MarkerItem().icon(R.mipmap.ic_launcher22 + "").position(latLng);
        markerLayer.add(markerItem);

        markerLayer.update();
    }


    /**
     * 描画一个文字背景
     *
     * @param c
     * @param roundPx
     * @return
     */
    private Bitmap getTextBitmap(@ColorInt int c, float roundPx) {

        Bitmap bitmap = Bitmap.createBitmap(200, 100,
                Bitmap.Config.ARGB_8888);
        bitmap.eraseColor(c);

        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        {
            int width = 200;
            final int mcolor = 0xffdddddd;
            int height = 100;

            final RectF rectFf = new RectF(0.5f, 0.5f, width - 0.5f, height - 0.5f);

            Paint mpaint = new Paint();
            mpaint.setColor(mcolor);
            mpaint.setStyle(Paint.Style.STROKE);
            mpaint.setStrokeWidth(1);
            canvas.drawRoundRect(rectFf, 20, 20, mpaint);
        }
        return output;
    }

    private MarkerLayer markerLayer;// MarkerLayer
    private Bitmap icon = null;

    MarkerLayerOptions.MarkerItem markerItem;

    //加点
    public void addmarklayer(View view) {
        //绘制marklayer
        LatLng latLng = RandomUtil.randomLatLon();

        markerItem = new MarkerLayerOptions.MarkerItem().icon(R.mipmap.ic_launcher22 + "").position(latLng);

        markerLayer.add(markerItem);
        markerLayer.update();

    }

    public void changeStyle() {

        mineMap.setStyleUrl("http://minedata.cn/service/solu/style/id/4634", new MineMap.OnStyleLoadedListener() {
            @Override
            public void onStyleLoaded(String s) {

                //mine map sdk 2.0.6及之后版本这样使用
                //layers 和 images 都在这里面加载
                addImageToMap();

                addLayersToMap();
            }
        });
    }


    //生命周期
    @Override
    protected void onStart() {
        super.onStart();
        if (mapView != null)
            mapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mapView != null)
            mapView.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mapView != null)
            mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mapView != null)
            mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //2.0.6及之后版本 需要将加入地图的图片资源进行清理
        if (mineMap != null)
            mineMap.removeImagesAll();


        if (mapView != null)
            mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        if (mapView != null) {
            mapView.onLowMemory();
        }
    }
}
